//
//  VerPwdController.m
//  SafPay
//
//  Created by  ztryxh on 2018/6/25.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "VerPwdController.h"
#import "CodeView.h"
#import "WalletDetailsController.h"
#import "SetPwdController.h"
@interface VerPwdController ()

@property (nonatomic,strong) TRRequest *request;
@property (nonatomic,copy) NSString *pwd;
@end

@implementation VerPwdController
#pragma mark -Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"验证安全密码";
    [self initUI];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Privacy
- (void)initUI
{
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth, 108)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UILabel *lab = [UILabel labelWithFrame:CGRectMake(12, 20, kScreenWidth - 24, 14) font:kFONT(15) color:rgb(58,58,58) text:@"输入密码"];
    if(self.isSetting){
        [lab setText:@"请输入旧密码"];
    }
    lab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lab];
    lab.textAlignment = NSTextAlignmentCenter;
    CodeView *codeView = [[CodeView alloc] initWithFrame:CGRectMake(60, lab.yzBottom + 5, kScreenWidth - 120, (kScreenWidth - 120)/6)  num:6 lineColor:[UIColor blackColor] textFont:14];
    codeView.hasSpaceLine = NO;
    //输入风格
    codeView.codeType = CodeViewTypeCustom;
    codeView.hasUnderLine = YES;
    codeView.noInputAni = NO;
    codeView.codeType = CodeViewTypeSecret;
     [codeView beginEdit];
    [view addSubview:codeView];
    
  
    
    kWeakSelf(weakSelf);
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    btn.titleLabel.font = kFONT(16);
    btn.backgroundColor = rgb(33,177,227);
    [self.view addSubview:btn];
   
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
     
        [self verPwd:weakSelf.pwd];
      
    }];
    codeView.EditingBlcok = ^(NSString *text) {
      
        if(text.length<6){
            
            [btn setUserInteractionEnabled:NO];
            [btn setBackgroundColor:kGrayColor];
        }else{
            
            [btn setUserInteractionEnabled:YES];
            [btn setBackgroundColor:rgb(33,177,227)];
        }
    };
   
    codeView.EndEditBlcok = ^(NSString *text) {
        
        weakSelf.pwd=text;
        [self verPwd:weakSelf.pwd];
    };
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    
}

#pragma mark - NewWork
-(void)verPwd:(NSString *)str{
    
   
    NSString *path=[NSString stringWithFormat:@"%@%@",kBaseUrl,kVerPwd];
    NSMutableDictionary *dict=[NSMutableDictionary new];
    [dict setValue:str forKey:@"password"];
    [dict setValue:[AccountTools shared].currentAccount.userId forKey:@"uid"];
    [dict setValue:[AccountTools shared].currentAccount.token forKey:@"token"];
    [[TRRequest request] Post:path parameters:dict success:^(TRRequest *request, NSDictionary *responseDict) {
        
        
        MyLog(@"%@",responseDict);
        if([[responseDict valueForKey:@"error"] integerValue]==0){
            
            if(self.isSetting){
                
                SetPwdController *vc=[SetPwdController new];
                vc.isSetting=YES;
                vc.isChange=YES;
                [self.navigationController pushViewController:vc animated:YES];
                
            }else{
                
                 [self turnOutNetWork];
                
            }
           
            
            
        }else{
            
            
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
        
    } failure:^(TRRequest *request, NSError *error) {
        
        
        [MBProgressHUD showErrorWithText:@"网络请求失败！"];
    }];
}
-(void)turnOutNetWork{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.myModel.type forKey:@"coinname"];
    [dic setValue:[AccountTools shared].currentAccount.token forKey:@"token"];
    [dic setValue:[AccountTools shared].currentAccount.userId forKey:@"uid"];
    [dic setValue:self.myModel.addressModel.address forKey:@"address"];
    [dic setValue:self.myModel.amount forKey:@"amount"];
    
    [[TRRequest request] Post:kSetURL(kTransferOut) parameters:dic success:^(TRRequest *request, NSDictionary *responseDict) {
        
        if ([[responseDict valueForKey:@"error"] integerValue] == 0) {
//            [self.navigationController popViewControllerAnimated:YES];
            [MBProgressHUD showSuccessWithText:@"转出成功"];
            [self.navigationController.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                if([obj isKindOfClass:[WalletDetailsController class]]){
                    
                    [self.navigationController popToViewController:obj animated:YES];
                }
                
            }];
            
        }else
        {
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
    } failure:^(TRRequest *request, NSError *error) {
        
        [MBProgressHUD showErrorWithText:@"网络连接失败！"];
    }];
}
@end
